package com.sample.javabean;
/**
 * 报销信息类
 * @author 深深
 * @version 1.0
 */
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sample.action.form.BxmessageForm;
import com.sample.jdbc.JdbcUtils;
import com.sample.util.Pager;


public class Bxmessage {
	private static final Log LOG=LogFactory.getLog(Bxmessage.class);
	private JdbcUtils jdbcUtils;
	private String cardNo;//身份证号
	private String persName;//参合人员姓名
	private String sex;//性别
	private String illname;//疾病名称
	private String money;//总金额
	private double rate;//比例
	private String realmoney;//实际得到金额
	private String thetime;//报销年份
	private String full_name;//报销经办人
	private int id;//主键
	private double maxLine;//封顶线
	private String endday;//结束日期

	//get/set方法
	public String getCardNo() {
		return cardNo;
	}
	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}
	public String getPersName() {
		return persName;
	}
	public void setPersName(String persName) {
		this.persName = persName;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getIllname() {
		return illname;
	}
	public void setIllname(String illname) {
		this.illname = illname;
	}
	public String getMoney() {
		return money;
	}
	public void setMoney(String money) {
		this.money = money;
	}
	public double getRate() {
		return rate;
	}
	public void setRate(double rate) {
		this.rate = rate;
	}

	public String getRealmoney() {
		return realmoney;
	}
	public void setRealmoney(String realmoney) {
		this.realmoney = realmoney;
	}
	public String getThetime() {
		return thetime;
	}
	public void setThetime(String thetime) {
		this.thetime = thetime;
	}

	public String getFull_name() {
		return full_name;
	}
	public void setFull_name(String full_name) {
		this.full_name = full_name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}

	public double getMaxLine() {
		return maxLine;
	}
	public void setMaxLine(double maxLine) {
		this.maxLine = maxLine;
	}

	public String getEndday() {
		return endday;
	}
	public void setEndday(String endday) {
		this.endday = endday;
	}

	//有参构造方法
	public Bxmessage(String cardNo, String persName, String sex,
			String illname, String money, double rate, String realmoney,
			String thetime, String full_name, int id) {
		super();
		this.cardNo = cardNo;
		this.persName = persName;
		this.sex = sex;
		this.illname = illname;
		this.money = money;
		this.rate = rate;
		this.realmoney = realmoney;
		this.thetime = thetime;
		this.full_name = full_name;
		this.id = id;
	}


	public Bxmessage() {
		jdbcUtils =new JdbcUtils();
	}

	/**
	 * 添加报销信息
	 */
	public void add(){
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs =null;
		try{
			conn =jdbcUtils.getConn();
			stmt =conn.createStatement();

			String sql1="select * from t_persinfo where cardNo='"+this.cardNo+"'";
			rs=stmt.executeQuery(sql1);
			if(rs.next()){
				this.persName =rs.getString("persName");
				this.sex =rs.getString("sex");
			}

			String sql2="select * from t_mxill_message where cardNo='"+this.cardNo+"'";
			rs=stmt.executeQuery(sql2);
			if(rs.next()){
				this.money =rs.getString("money");
				this.illname =rs.getString("illname");
			}
			String a=money;
			double aa=Double.parseDouble(a);
			System.out.println(+aa);
			//查询报销政策的年份，慢性病证是否过期
			String sql3="select * from zc_policy where runyear='"+this.thetime+"'";
			rs=stmt.executeQuery(sql3);
			if(rs.next()){
				this.rate =rs.getDouble("rate");
				this.maxLine =rs.getDouble("maxLine");
			}
			double b=rate;
			double d=aa*b;
			double z;
			System.out.println(+d);
			if(d>maxLine){
				z=maxLine;
			}
			else{
				z=d;
			}
			//查询慢性病报销账单的身份证是否一致
			String sql4="select * from t_mbbill where cardNo='"+this.cardNo+"'";
			rs=stmt.executeQuery(sql4);	
			if(rs.next()){
				this.endday =rs.getString("endday");
			}
			String x=endday;
			double xx=Double.parseDouble(x);
			System.out.println(+xx);
			String q=thetime;
			double qq=Double.parseDouble(q);
			System.out.println(+qq);
			if(qq<=xx){
				String sql="insert into t_bx_message(cardNo,full_name,thetime,persName,sex,money,illname,rate,realmoney) values('"+this.cardNo+"','"+this.full_name+"','"+this.thetime+"','"+this.persName+"','"+this.sex+"','"+this.money+"','"+this.illname+"','"+this.rate+"','"+z+"')";
				//添加报销信息
				stmt.executeUpdate(sql);
			}

		}catch (Exception e) {
			e.printStackTrace();

		}finally{
			jdbcUtils.close(conn, stmt);

		}

	}
	/**
	 * 分页显示报销信息
	 * @param page
	 * @param size
	 * @param form
	 * @return
	 */
	public Pager<Bxmessage> list(int page,int size,BxmessageForm form) {

		Connection conn = null;
		Statement stmt = null;
		ResultSet rs =null;
		Pager<Bxmessage> results=new Pager<Bxmessage>();
		results.setPage(page);
		results.setSize(size);
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			int start=(page-1)*size;
			int limit=size;
			//模糊查询参合人员姓名
			String selectSql="select * from t_bx_message where 1=1";
			if(form!=null&&form.getPersName()!=null&&!"".equals(form.getPersName())){
				selectSql +=" and persName like'%"+form.getPersName()+"%'";
			}


			String countSql ="select count(*)as c from ("+selectSql+") as t1";
			
			String pageSql=selectSql+" limit "+start+","+limit;
			rs= stmt.executeQuery(countSql);
			int total=0;
			while(rs.next()){
				total=rs.getInt("c");
			}
			results.setTotal(total);

			rs= stmt.executeQuery(pageSql);
			while(rs.next()){


				String cardNo=rs.getString("cardNo");
				String persName=rs.getString("persName");
				String sex=rs.getString("sex");
				String illname=rs.getString("illname");
				String money=rs.getString("money");
				double rate=rs.getDouble("rate");
				String realmoney=rs.getString("realmoney");
				String thetime=rs.getString("thetime");
				String full_name=rs.getString("full_name");
				int id=rs.getInt("id");



				Bxmessage model=new Bxmessage();
				model.setCardNo(cardNo);
				model.setPersName(persName);
				model.setSex(sex);
				model.setIllname(illname);
				model.setMoney(money);
				model.setRate(rate);
				model.setRealmoney(realmoney);
				model.setThetime(thetime);
				model.setFull_name(full_name);
				model.setId(id);
				results.getDataSets().add(model);
			}
			results.init();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			jdbcUtils.close(conn, stmt);
		}
		return results;

	}
	/**
	 * 删除报销信息
	 */
	public void delBxmessage() {

		Connection conn = null;
		Statement stmt = null;
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();

			String sql = "delete from t_bx_message  where id='"+this.id+"'";

			System.out.println("del t_bx_message");
			stmt.executeUpdate(sql);

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			jdbcUtils.close(conn, stmt);
		}

	}


}
